package com.kgc.server.controller;

import com.kgc.server.service.Impl.InterfaceSecurityServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Controller
public class InterfaceSecurityController {
    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private HttpServletRequest request;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private InterfaceSecurityServiceImpl interfaceSecurityService;

    @RequestMapping("setRedisNum")
    @ResponseBody
    @CrossOrigin
    public String setRedisNum(){
        logger.info("InterfaceSecurityController setRedisNum start");
        String str = interfaceSecurityService.setRedisNum();
        logger.info("InterfaceSecurityController setRedisNum str:"+str);
        return str;
    }

    @RequestMapping("returnInterfaceStr")
    @ResponseBody
    @CrossOrigin
    public String returnInterfaceStr(HttpServletRequest request){
        logger.info("InterfaceSecurityController setRedisNum request:"+request);
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, String[]> map = new TreeMap<String, String[]>(parameterMap);
        logger.info("InterfaceSecurityController setRedisNum request:"+request+",parameterMap:"+parameterMap+",map:"+map);
        long TimeStamp = Long.parseLong(request.getHeader("TimeStamp"));//获取前端传的时间戳
        logger.info("InterfaceSecurityController setRedisNum request:"+request+",parameterMap:"+parameterMap+",map:"+map+",TimeStamp:"+TimeStamp);
        String randomNum = request.getHeader("randomNum");//获取前端传的随机数
        logger.info("InterfaceSecurityController setRedisNum request:"+request+",parameterMap:"+parameterMap+",map:"+map+",TimeStamp:"+TimeStamp+",randomNum:"+randomNum);
        String encryptionStr = request.getHeader("encryptionStr");//获取前端传的加密字符串
        logger.info("InterfaceSecurityController setRedisNum request:"+request+",parameterMap:"+parameterMap+",map:"+map+",TimeStamp:"+TimeStamp+",randomNum:"+randomNum+",encryptionStr:"+encryptionStr);
        String str = interfaceSecurityService.returnInterfaceStr(map,TimeStamp,randomNum,encryptionStr);
        logger.info("InterfaceSecurityController setRedisNum request:"+request+",parameterMap:"+parameterMap+",map:"+map+",TimeStamp:"+TimeStamp+",randomNum:"+randomNum+",encryptionStr:"+encryptionStr+",str:"+str);
        return str;
    }
}
